package com.raining.inningz.database.dao;

import java.util.List;
import java.util.ArrayList;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteStatement;

import de.greenrobot.dao.AbstractDao;
import de.greenrobot.dao.Property;
import de.greenrobot.dao.internal.SqlUtils;
import de.greenrobot.dao.internal.DaoConfig;
import de.greenrobot.dao.query.Query;
import de.greenrobot.dao.query.QueryBuilder;

import com.raining.inningz.database.dao.MatchInnings;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table MATCH_INNINGS.
*/
public class MatchInningsDao extends AbstractDao<MatchInnings, Long> {

    public static final String TABLENAME = "MATCH_INNINGS";

    /**
     * Properties of entity MatchInnings.<br/>
     * Can be used for QueryBuilder and for referencing column names.
    */
    public static class Properties {
        public final static Property Id = new Property(0, Long.class, "id", true, "_id");
        public final static Property BattingTeamNum = new Property(1, Long.class, "battingTeamNum", false, "BATTING_TEAM_NUM");
        public final static Property BowlingTeamNum = new Property(2, Long.class, "bowlingTeamNum", false, "BOWLING_TEAM_NUM");
        public final static Property InProgress = new Property(3, Boolean.class, "inProgress", false, "IN_PROGRESS");
        public final static Property InningNum = new Property(4, Long.class, "inningNum", false, "INNING_NUM");
        public final static Property LastBallNum = new Property(5, Long.class, "lastBallNum", false, "LAST_BALL_NUM");
        public final static Property LastBallNumSeq = new Property(6, Long.class, "lastBallNumSeq", false, "LAST_BALL_NUM_SEQ");
        public final static Property SeqNum = new Property(7, Long.class, "seqNum", false, "SEQ_NUM");
        public final static Property Total = new Property(8, Long.class, "total", false, "TOTAL");
        public final static Property Undef1 = new Property(9, Long.class, "undef1", false, "UNDEF1");
        public final static Property UploadedSw = new Property(10, Long.class, "uploadedSw", false, "UPLOADED_SW");
        public final static Property Wickets = new Property(11, Long.class, "wickets", false, "WICKETS");
        public final static Property Overs = new Property(12, Float.class, "overs", false, "OVERS");
        public final static Property EndReasonCd = new Property(13, String.class, "endReasonCd", false, "END_REASON_CD");
        public final static Property EndReasonDesc = new Property(14, String.class, "endReasonDesc", false, "END_REASON_DESC");
        public final static Property Undef2 = new Property(15, String.class, "undef2", false, "UNDEF2");
        public final static Property Undef3 = new Property(16, String.class, "undef3", false, "UNDEF3");
        public final static Property Undef4 = new Property(17, String.class, "undef4", false, "UNDEF4");
        public final static Property ScorelineId = new Property(18, Long.class, "scorelineId", false, "SCORELINE_ID");
        public final static Property MatchInningExtrasSummaryId = new Property(19, Long.class, "matchInningExtrasSummaryId", false, "MATCH_INNING_EXTRAS_SUMMARY_ID");
        public final static Property MatchInningBattingSummaryId = new Property(20, Long.class, "matchInningBattingSummaryId", false, "MATCH_INNING_BATTING_SUMMARY_ID");
        public final static Property MatchInningBowlingSummaryId = new Property(21, Long.class, "matchInningBowlingSummaryId", false, "MATCH_INNING_BOWLING_SUMMARY_ID");
        public final static Property ScoreLineId = new Property(22, Long.class, "scoreLineId", false, "SCORE_LINE_ID");
        public final static Property InningsOfMatch = new Property(23, Long.class, "inningsOfMatch", false, "INNINGS_OF_MATCH");
        public final static Property MatchInningzId = new Property(24, Long.class, "matchInningzId", false, "MATCH_INNINGZ_ID");
    };

    private DaoSession daoSession;

    private Query<MatchInnings> matches_MatchInningsListQuery;

    public MatchInningsDao(DaoConfig config) {
        super(config);
    }
    
    public MatchInningsDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
        this.daoSession = daoSession;
    }

    /** Creates the underlying database table. */
    public static void createTable(SQLiteDatabase db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "'MATCH_INNINGS' (" + //
                "'_id' INTEGER PRIMARY KEY ," + // 0: id
                "'BATTING_TEAM_NUM' INTEGER," + // 1: battingTeamNum
                "'BOWLING_TEAM_NUM' INTEGER," + // 2: bowlingTeamNum
                "'IN_PROGRESS' INTEGER," + // 3: inProgress
                "'INNING_NUM' INTEGER," + // 4: inningNum
                "'LAST_BALL_NUM' INTEGER," + // 5: lastBallNum
                "'LAST_BALL_NUM_SEQ' INTEGER," + // 6: lastBallNumSeq
                "'SEQ_NUM' INTEGER," + // 7: seqNum
                "'TOTAL' INTEGER," + // 8: total
                "'UNDEF1' INTEGER," + // 9: undef1
                "'UPLOADED_SW' INTEGER," + // 10: uploadedSw
                "'WICKETS' INTEGER," + // 11: wickets
                "'OVERS' REAL," + // 12: overs
                "'END_REASON_CD' TEXT," + // 13: endReasonCd
                "'END_REASON_DESC' TEXT," + // 14: endReasonDesc
                "'UNDEF2' TEXT," + // 15: undef2
                "'UNDEF3' TEXT," + // 16: undef3
                "'UNDEF4' TEXT," + // 17: undef4
                "'SCORELINE_ID' INTEGER," + // 18: scorelineId
                "'MATCH_INNING_EXTRAS_SUMMARY_ID' INTEGER," + // 19: matchInningExtrasSummaryId
                "'MATCH_INNING_BATTING_SUMMARY_ID' INTEGER," + // 20: matchInningBattingSummaryId
                "'MATCH_INNING_BOWLING_SUMMARY_ID' INTEGER," + // 21: matchInningBowlingSummaryId
                "'SCORE_LINE_ID' INTEGER," + // 22: scoreLineId
                "'INNINGS_OF_MATCH' INTEGER," + // 23: inningsOfMatch
                "'MATCH_INNINGZ_ID' INTEGER);"); // 24: matchInningzId
    }

    /** Drops the underlying database table. */
    public static void dropTable(SQLiteDatabase db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "'MATCH_INNINGS'";
        db.execSQL(sql);
    }

    /** @inheritdoc */
    @Override
    protected void bindValues(SQLiteStatement stmt, MatchInnings entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
 
        Long battingTeamNum = entity.getBattingTeamNum();
        if (battingTeamNum != null) {
            stmt.bindLong(2, battingTeamNum);
        }
 
        Long bowlingTeamNum = entity.getBowlingTeamNum();
        if (bowlingTeamNum != null) {
            stmt.bindLong(3, bowlingTeamNum);
        }
 
        Boolean inProgress = entity.getInProgress();
        if (inProgress != null) {
            stmt.bindLong(4, inProgress ? 1l: 0l);
        }
 
        Long inningNum = entity.getInningNum();
        if (inningNum != null) {
            stmt.bindLong(5, inningNum);
        }
 
        Long lastBallNum = entity.getLastBallNum();
        if (lastBallNum != null) {
            stmt.bindLong(6, lastBallNum);
        }
 
        Long lastBallNumSeq = entity.getLastBallNumSeq();
        if (lastBallNumSeq != null) {
            stmt.bindLong(7, lastBallNumSeq);
        }
 
        Long seqNum = entity.getSeqNum();
        if (seqNum != null) {
            stmt.bindLong(8, seqNum);
        }
 
        Long total = entity.getTotal();
        if (total != null) {
            stmt.bindLong(9, total);
        }
 
        Long undef1 = entity.getUndef1();
        if (undef1 != null) {
            stmt.bindLong(10, undef1);
        }
 
        Long uploadedSw = entity.getUploadedSw();
        if (uploadedSw != null) {
            stmt.bindLong(11, uploadedSw);
        }
 
        Long wickets = entity.getWickets();
        if (wickets != null) {
            stmt.bindLong(12, wickets);
        }
 
        Float overs = entity.getOvers();
        if (overs != null) {
            stmt.bindDouble(13, overs);
        }
 
        String endReasonCd = entity.getEndReasonCd();
        if (endReasonCd != null) {
            stmt.bindString(14, endReasonCd);
        }
 
        String endReasonDesc = entity.getEndReasonDesc();
        if (endReasonDesc != null) {
            stmt.bindString(15, endReasonDesc);
        }
 
        String undef2 = entity.getUndef2();
        if (undef2 != null) {
            stmt.bindString(16, undef2);
        }
 
        String undef3 = entity.getUndef3();
        if (undef3 != null) {
            stmt.bindString(17, undef3);
        }
 
        String undef4 = entity.getUndef4();
        if (undef4 != null) {
            stmt.bindString(18, undef4);
        }
 
        Long scorelineId = entity.getScorelineId();
        if (scorelineId != null) {
            stmt.bindLong(19, scorelineId);
        }
 
        Long matchInningExtrasSummaryId = entity.getMatchInningExtrasSummaryId();
        if (matchInningExtrasSummaryId != null) {
            stmt.bindLong(20, matchInningExtrasSummaryId);
        }
 
        Long matchInningBattingSummaryId = entity.getMatchInningBattingSummaryId();
        if (matchInningBattingSummaryId != null) {
            stmt.bindLong(21, matchInningBattingSummaryId);
        }
 
        Long matchInningBowlingSummaryId = entity.getMatchInningBowlingSummaryId();
        if (matchInningBowlingSummaryId != null) {
            stmt.bindLong(22, matchInningBowlingSummaryId);
        }
 
        Long scoreLineId = entity.getScoreLineId();
        if (scoreLineId != null) {
            stmt.bindLong(23, scoreLineId);
        }
 
        Long inningsOfMatch = entity.getInningsOfMatch();
        if (inningsOfMatch != null) {
            stmt.bindLong(24, inningsOfMatch);
        }
    }

    @Override
    protected void attachEntity(MatchInnings entity) {
        super.attachEntity(entity);
        entity.__setDaoSession(daoSession);
    }

    /** @inheritdoc */
    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }    

    /** @inheritdoc */
    @Override
    public MatchInnings readEntity(Cursor cursor, int offset) {
        MatchInnings entity = new MatchInnings( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
            cursor.isNull(offset + 1) ? null : cursor.getLong(offset + 1), // battingTeamNum
            cursor.isNull(offset + 2) ? null : cursor.getLong(offset + 2), // bowlingTeamNum
            cursor.isNull(offset + 3) ? null : cursor.getShort(offset + 3) != 0, // inProgress
            cursor.isNull(offset + 4) ? null : cursor.getLong(offset + 4), // inningNum
            cursor.isNull(offset + 5) ? null : cursor.getLong(offset + 5), // lastBallNum
            cursor.isNull(offset + 6) ? null : cursor.getLong(offset + 6), // lastBallNumSeq
            cursor.isNull(offset + 7) ? null : cursor.getLong(offset + 7), // seqNum
            cursor.isNull(offset + 8) ? null : cursor.getLong(offset + 8), // total
            cursor.isNull(offset + 9) ? null : cursor.getLong(offset + 9), // undef1
            cursor.isNull(offset + 10) ? null : cursor.getLong(offset + 10), // uploadedSw
            cursor.isNull(offset + 11) ? null : cursor.getLong(offset + 11), // wickets
            cursor.isNull(offset + 12) ? null : cursor.getFloat(offset + 12), // overs
            cursor.isNull(offset + 13) ? null : cursor.getString(offset + 13), // endReasonCd
            cursor.isNull(offset + 14) ? null : cursor.getString(offset + 14), // endReasonDesc
            cursor.isNull(offset + 15) ? null : cursor.getString(offset + 15), // undef2
            cursor.isNull(offset + 16) ? null : cursor.getString(offset + 16), // undef3
            cursor.isNull(offset + 17) ? null : cursor.getString(offset + 17), // undef4
            cursor.isNull(offset + 18) ? null : cursor.getLong(offset + 18), // scorelineId
            cursor.isNull(offset + 19) ? null : cursor.getLong(offset + 19), // matchInningExtrasSummaryId
            cursor.isNull(offset + 20) ? null : cursor.getLong(offset + 20), // matchInningBattingSummaryId
            cursor.isNull(offset + 21) ? null : cursor.getLong(offset + 21), // matchInningBowlingSummaryId
            cursor.isNull(offset + 22) ? null : cursor.getLong(offset + 22), // scoreLineId
            cursor.isNull(offset + 23) ? null : cursor.getLong(offset + 23) // inningsOfMatch
        );
        return entity;
    }
     
    /** @inheritdoc */
    @Override
    public void readEntity(Cursor cursor, MatchInnings entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setBattingTeamNum(cursor.isNull(offset + 1) ? null : cursor.getLong(offset + 1));
        entity.setBowlingTeamNum(cursor.isNull(offset + 2) ? null : cursor.getLong(offset + 2));
        entity.setInProgress(cursor.isNull(offset + 3) ? null : cursor.getShort(offset + 3) != 0);
        entity.setInningNum(cursor.isNull(offset + 4) ? null : cursor.getLong(offset + 4));
        entity.setLastBallNum(cursor.isNull(offset + 5) ? null : cursor.getLong(offset + 5));
        entity.setLastBallNumSeq(cursor.isNull(offset + 6) ? null : cursor.getLong(offset + 6));
        entity.setSeqNum(cursor.isNull(offset + 7) ? null : cursor.getLong(offset + 7));
        entity.setTotal(cursor.isNull(offset + 8) ? null : cursor.getLong(offset + 8));
        entity.setUndef1(cursor.isNull(offset + 9) ? null : cursor.getLong(offset + 9));
        entity.setUploadedSw(cursor.isNull(offset + 10) ? null : cursor.getLong(offset + 10));
        entity.setWickets(cursor.isNull(offset + 11) ? null : cursor.getLong(offset + 11));
        entity.setOvers(cursor.isNull(offset + 12) ? null : cursor.getFloat(offset + 12));
        entity.setEndReasonCd(cursor.isNull(offset + 13) ? null : cursor.getString(offset + 13));
        entity.setEndReasonDesc(cursor.isNull(offset + 14) ? null : cursor.getString(offset + 14));
        entity.setUndef2(cursor.isNull(offset + 15) ? null : cursor.getString(offset + 15));
        entity.setUndef3(cursor.isNull(offset + 16) ? null : cursor.getString(offset + 16));
        entity.setUndef4(cursor.isNull(offset + 17) ? null : cursor.getString(offset + 17));
        entity.setScorelineId(cursor.isNull(offset + 18) ? null : cursor.getLong(offset + 18));
        entity.setMatchInningExtrasSummaryId(cursor.isNull(offset + 19) ? null : cursor.getLong(offset + 19));
        entity.setMatchInningBattingSummaryId(cursor.isNull(offset + 20) ? null : cursor.getLong(offset + 20));
        entity.setMatchInningBowlingSummaryId(cursor.isNull(offset + 21) ? null : cursor.getLong(offset + 21));
        entity.setScoreLineId(cursor.isNull(offset + 22) ? null : cursor.getLong(offset + 22));
        entity.setInningsOfMatch(cursor.isNull(offset + 23) ? null : cursor.getLong(offset + 23));
     }
    
    /** @inheritdoc */
    @Override
    protected Long updateKeyAfterInsert(MatchInnings entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }
    
    /** @inheritdoc */
    @Override
    public Long getKey(MatchInnings entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    /** @inheritdoc */
    @Override    
    protected boolean isEntityUpdateable() {
        return true;
    }
    
    /** Internal query to resolve the "matchInningsList" to-many relationship of Matches. */
    public List<MatchInnings> _queryMatches_MatchInningsList(Long matchInningzId) {
        synchronized (this) {
            if (matches_MatchInningsListQuery == null) {
                QueryBuilder<MatchInnings> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.MatchInningzId.eq(null));
                matches_MatchInningsListQuery = queryBuilder.build();
            }
        }
        Query<MatchInnings> query = matches_MatchInningsListQuery.forCurrentThread();
        query.setParameter(0, matchInningzId);
        return query.list();
    }

    private String selectDeep;

    protected String getSelectDeep() {
        if (selectDeep == null) {
            StringBuilder builder = new StringBuilder("SELECT ");
            SqlUtils.appendColumns(builder, "T", getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T0", daoSession.getScoreLineDao().getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T1", daoSession.getMatchesDao().getAllColumns());
            builder.append(" FROM MATCH_INNINGS T");
            builder.append(" LEFT JOIN SCORE_LINE T0 ON T.'SCORE_LINE_ID'=T0.'_id'");
            builder.append(" LEFT JOIN MATCHES T1 ON T.'INNINGS_OF_MATCH'=T1.'_id'");
            builder.append(' ');
            selectDeep = builder.toString();
        }
        return selectDeep;
    }
    
    protected MatchInnings loadCurrentDeep(Cursor cursor, boolean lock) {
        MatchInnings entity = loadCurrent(cursor, 0, lock);
        int offset = getAllColumns().length;

        ScoreLine scoreLine = loadCurrentOther(daoSession.getScoreLineDao(), cursor, offset);
        entity.setScoreLine(scoreLine);
        offset += daoSession.getScoreLineDao().getAllColumns().length;

        Matches matches = loadCurrentOther(daoSession.getMatchesDao(), cursor, offset);
        entity.setMatches(matches);

        return entity;    
    }

    public MatchInnings loadDeep(Long key) {
        assertSinglePk();
        if (key == null) {
            return null;
        }

        StringBuilder builder = new StringBuilder(getSelectDeep());
        builder.append("WHERE ");
        SqlUtils.appendColumnsEqValue(builder, "T", getPkColumns());
        String sql = builder.toString();
        
        String[] keyArray = new String[] { key.toString() };
        Cursor cursor = db.rawQuery(sql, keyArray);
        
        try {
            boolean available = cursor.moveToFirst();
            if (!available) {
                return null;
            } else if (!cursor.isLast()) {
                throw new IllegalStateException("Expected unique result, but count was " + cursor.getCount());
            }
            return loadCurrentDeep(cursor, true);
        } finally {
            cursor.close();
        }
    }
    
    /** Reads all available rows from the given cursor and returns a list of new ImageTO objects. */
    public List<MatchInnings> loadAllDeepFromCursor(Cursor cursor) {
        int count = cursor.getCount();
        List<MatchInnings> list = new ArrayList<MatchInnings>(count);
        
        if (cursor.moveToFirst()) {
            if (identityScope != null) {
                identityScope.lock();
                identityScope.reserveRoom(count);
            }
            try {
                do {
                    list.add(loadCurrentDeep(cursor, false));
                } while (cursor.moveToNext());
            } finally {
                if (identityScope != null) {
                    identityScope.unlock();
                }
            }
        }
        return list;
    }
    
    protected List<MatchInnings> loadDeepAllAndCloseCursor(Cursor cursor) {
        try {
            return loadAllDeepFromCursor(cursor);
        } finally {
            cursor.close();
        }
    }
    

    /** A raw-style query where you can pass any WHERE clause and arguments. */
    public List<MatchInnings> queryDeep(String where, String... selectionArg) {
        Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg);
        return loadDeepAllAndCloseCursor(cursor);
    }
 
}
